/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import hibernate.User;

/**
 *
 * @author Bespechniy
 */

@ManagedBean
@RequestScoped
public class UserSession implements Serializable {
    
    public UserSession() {}
    
    public User getCurrentUser() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
                .getExternalContext().getSession(true);
        return (User)session.getAttribute("user");
    }

    public void setCurrentUser(User u) {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
                .getExternalContext().getSession(true);
        session.setAttribute("user", u);
    }
    
    public void logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
                .getExternalContext().getSession(true);
        session.setAttribute("user", null);
    }
    
    public boolean isLogedIn() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
                .getExternalContext().getSession(true);
        User u = (User)session.getAttribute("user");
        return u != null;
    }
    
    public int getRole() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
                .getExternalContext().getSession(true);
        User u = (User)session.getAttribute("user");
        return u.getRole().getId();
    }
}